/**
 * 验证有效括号对
 * (],{},([{}]),(}),]
 */
const rule = {
    '(': ')',
    '{': '}',
    '[': ']',
}
const rights = Object.values(rule)

isValid = function (str) {
    const rightCon = []
    const containt = []
    Array.from(str).forEach(item => {
        if (rule[item]) {
            containt.push(item)
        }

        if (rights.indexOf(item) > -1) {
            const end = containt[containt.length - 1]
            if (rule[end] === item) {
                containt.pop()
            } else {
                rightCon.push(item)
            }
        }
    })

    return !containt.length && !rightCon.length
}

function isValid2(s) {

    const arr = Array.from(s)
    const containt = []
    for (let i = 0; i < arr.length; i++) {
        if (rule[arr[i]]) {
            containt.push(arr[i])
        }
        if (rights.indexOf(arr[i]) > -1) {
            const end = containt[containt.length - 1]
            if (rule[end] === arr[i]) {
                containt.pop()
            } else {
                return false
            }
        }
    }

    return !containt.length
}

console.time('isValid2')
isValid2('[]{}()2232222{]') //0.0380859375ms
console.timeEnd('isValid2')

console.time('isValid')
isValid('[]{}()2232222{]') //0.0361328125ms
console.timeEnd('isValid')